<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <link rel="stylesheet" th:href="@{/static/element/index.css}"/>
    <style>
        .time {
            font-size: 13px;
            color: #999;
        }

        .bottom {
            margin-top: 13px;
            line-height: 12px;
        }

        .button {
            padding: 5px;
            float: right;
        }

        .image {
            width: 260px;
            height:300px;
            display: block;
        }

        .clearfix:before,
        .clearfix:after {
            display: table;
            content: "";
        }

        .clearfix:after {
            clear: both
        }
    </style>
</head>
<body>
<div id="app">
    <template>
        <el-card style="background-color: #f7f7f7">
            <el-form :model="queryForm" ref="queryForm" :inline="true" label-width="100px" @submit.native.prevent>
                <el-form-item label="产品名称" prop="productName">
                    <el-input
                            v-model="queryForm.productName"
                            placeholder="请输入产品名称"
                            clearable
                            size="small"
                            @keyup.enter.native="handleQuery"
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">清空</el-button>
                </el-form-item>
            </el-form>
        </el-card>

        <el-row style="margin-top: 1%">
            <el-col :span="1.5">
            </el-col>
        </el-row>
        <div style="width:100%;height: 40px;display: flex;justify-content: flex-end;align-items: center;padding-right:20px">
            <el-button type="primary" size="mini" @click="openOrder">立即下单</el-button>
        </div>
        <el-dialog title="新建订单" :visible="dialogVisible" width="30%" @close="dialogVisible = false">
            <el-form  :model="orderForm" label-width="80px">
                <!-- <el-form-item label="活动名称">
        <el-input v-model="orderForm.name"></el-input>
      </el-form-item>
      <el-form-item label="活动名称">
        <el-input v-model="orderForm.name"></el-input>
      </el-form-item> -->
                <el-form-item label="电话号码">
                    <el-input v-model="orderForm.telephone"></el-input>
                </el-form-item>
                <el-form-item label="送货地址">
                    <el-input v-model="orderForm.address" type="textarea"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="comfirmOrder">确 定</el-button>
        </span>
        </el-dialog>
        <div style="width:100%;height:100%;display: flex;justify-content:space-evenly;align-items: center;flex-wrap: wrap;margin-top: 20px">
            <el-card :body-style="{ padding: '0px' }" v-for="(item,key) in tableData" :key="key">
                <img :src="item.picture" class="image">
                <div style="padding: 14px;">
                    <span>{{item.productName }}</span>
                    <div class="bottom clearfix">
                        <time class="time">加入购物车时间：{{ item.createTime }}</time>
                        <br/>
                        <div style="display: flex;justify-content: space-between;align-items: center;padding: 0px 5px;margin-top:10px">
                            <el-input-number v-model="item.total" @change="(num)=>{handleChange(num,item)}" :min="1" size="mini" label=""></el-input-number>


                            <el-button type="danger" class="button" @click="handleDelete(item)" size="mini" style="">删除</el-button>
                        </div>

                    </div>
                </div>
            </el-card>
        </div>
<!--        <el-table-->
<!--                v-loading="loading"-->
<!--                :data="tableData" row-key="id"-->
<!--                :header-cell-style="{'text-align':'center','color':'#303133','font-weight':'600','background-color': '#F5F7FA'}"-->
<!--                :cell-style="{'text-align':'center'}" border style="margin-top: 10px"-->
<!--                @selection-change="handleSelectionChange">-->
<!--            <el-table-column type="selection" align="center"></el-table-column>-->
<!--            <el-table-column label="ID" align="center" prop="id"></el-table-column>-->
<!--            <el-table-column label="用户姓名" align="center" prop="userName"></el-table-column>-->
<!--            <el-table-column label="产品名称" align="center" prop="productName"></el-table-column>-->
<!--            <el-table-column label="数量" align="center" prop="total">-->
<!--            </el-table-column>-->
<!--            <el-table-column label="图片" align="center" prop="picture"></el-table-column>-->
<!--            <el-table-column label="创建时间" align="center" prop="createTime" show-overflow-tooltip="true">-->
<!--                <template slot-scope="scope">-->
<!--                    <span>{{ scope.row.createTime}}</span>-->
<!--                </template>-->
<!--            </el-table-column>-->
<!--            <el-table-column label="操作" width="200">-->
<!--                <template slot-scope="scope">-->
<!--                    <el-button v-if="button.update" size="mini" type="primary" icon="el-icon-edit"-->
<!--                               @click="handleAddOrUpdate(2,scope.row)">修改-->
<!--                    </el-button>-->

<!--                    <el-button v-if="button.delete" size="mini" type="info" icon="el-icon-delete"-->
<!--                               @click="handleDelete(scope.row)">删除-->
<!--                    </el-button>-->
<!--                </template>-->
<!--            </el-table-column>-->
<!--        </el-table>-->
<!--        <el-pagination-->
<!--                background-->
<!--                :page-sizes="[10, 20, 30, 40,50]"-->
<!--                layout="total, sizes, prev, pager, next, jumper"-->
<!--                :total="total"-->
<!--                :page-size.sync="pageSize"-->
<!--                :current-page.sync="currentPage"-->
<!--                @size-change="handleQuery"-->
<!--                @current-change="handleQuery"-->
<!--                style="float: right;margin-top: 10px"-->
<!--        />-->
    </template>
</div>
</body>
<script th:src="@{/static/js/axios.min.js}"></script>
<script th:src="@{/static/js/request.js}"></script>
<script th:src="@{/static/js/common.js}"></script>
<script th:src="@{/static/api/admin/sysShopCar.js}"></script>
<script th:src="@{/static/js/vue.min.js}"></script>
<script th:src="@{/static/element/index.js}"></script>
<script th:src="@{/static/js/jquery.min.js}"></script>
<script th:src="@{/static/layer/layer.js}"></script>
<script>
    new Vue({
        el: '#app',
        data() {
            return {
                dialogVisible: false,
                orderForm: {
                    totalPrice: '',
                    telephone: '',
                    address: '',
                    productSum: '',
                    productList: [],
                },
                currentPage: 1,
                pageSize: 10,
                total: 0,
                tableData: [],
                multipleSelection: [],
                button: {
                    add: false,
                    update: false,
                    delete: false
                },
                loading: false,
                queryForm: {
                    roleName: ''
                }
            }
        },
        created() {
            this.handleQuery();
            let sysButtons = JSON.parse(localStorage.getItem("mars-sysButtons"));
            this.button.add = sysButtons.indexOf("admin/sysShopCar/add") > -1;
            this.button.update = sysButtons.indexOf("admin/sysShopCar/update") > -1;
            this.button.delete = sysButtons.indexOf("admin/sysShopCar/delete") > -1;

        },
        methods: {
            openOrder() {
                this.dialogVisible = true;
                this.orderForm = {
                    // totalPrice: '',
                    telephone: '',
                    address: '',
                    productSum: '',
                    productList: [],
                };

            },
            comfirmOrder() {
                let params = {
                    ...this.orderForm,
                    isShopCar:1,
                };
                let productList = [];
                let productSum = 0;
                let totalPrice = 0;

                this.tableData.forEach(x => {
                    productList.push({
                        id: x.productId,
                        amount: x.total
                    });
                    productSum = productSum + x.total;
                    totalPrice = totalPrice + (x.total*x.price)
                });
                params.productSum = productSum;
                params.productList = productList;
                params.totalPrice = totalPrice;

                addOrder(params).then(res=>{
                    if(res.code == 200){
                        this.$notify({
                            title: '成功',
                            message: '下单成功！',
                            type: 'success'
                        });
                        this.dialogVisible = false;
                    }
                })
            },
            handleChange(num,item){
                console.log(num);
                addOrUpdate2('/admin/sysShopCar/update',
                    {formData:{
                            ...item
                        }});
            },
            handExportData() {
                let queryParam = this.queryForm
                queryParam.pageNo = this.currentPage
                queryParam.pageSize = -1
                exports('/admin/sysShopCar/export', queryParam).then(res => {
                    this.$notify({
                        title: '成功',
                        message: '导出成功',
                        type: 'success'
                    });
                });
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            batchHandleDelete() {
                let params = {}
                if (this.multipleSelection.length === 0) {
                    this.$notify({
                        title: '失败',
                        message: '请选择删除行',
                        duration: 2000
                    });
                    return
                }
                deleteById('/admin/sysShopCar/delete/' + this.multipleSelection.map((item) => item.id), this);
            },
            switchChange(row) {
                this.formData = row
                update(this.formData).then(res => {
                    if (res.code === '200') {
                        this.$notify({
                            title: '成功',
                            message: '操作成功',
                            type: 'success'
                        });
                    }
                })
            },

            resetQuery() {
                this.queryForm = {}
                this.handleQuery()
            },


            handleCurrentChange(val) {
                this.currentPage = val;
                this.handleQuery();
            },
            handleSizeChange(val) {
                this.pageSize = val;
                this.handleQuery();
            },
            handleQuery() {
                let queryParam = this.queryForm
                queryParam.pageNo = this.currentPage
                queryParam.pageSize = this.pageSize
                this.loading = true
                pageList(queryParam).then((res) => {
                    if (res.code === '200') {
                        this.tableData = res.data.list;
                        this.total = res.data.count;
                        this.loading = false
                    } else {
                        this.loading = false;
                        this.$notify({
                            title: '失败',
                            message: res.message,
                            type: 'error'
                        });
                    }
                });
            },
            handleDelete(row) {
                deleteById('/admin/sysShopCar/delete/' + row.id, this);
            },

            handleAddOrUpdate(type, row) {
                if (type === 1) {
                    openPage('新增购物车', '/admin/sysShopCar/sysShopCarAddUpdate.html', this);
                } else {
                    openPage('修改购物车', '/admin/sysShopCar/sysShopCarAddUpdate.html?id=' + row.id, this);
                }
            },
        }
    });
</script>
</html>
